//
//  LoanDetailsTextView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/12.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class LoanDetailsTextView: UIView {
    
    fileprivate var model : GenericLoansModel!
    
    fileprivate let fontSize = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
    
    fileprivate let amountLabel = UILabel() //贷款金额
    fileprivate let timeLabel = UILabel() //贷款期限
    fileprivate let amountTextView = UITextField.init(Width: 5) //贷款金额数
    fileprivate let timeTextView = UITextField.init(Width: 5) //贷款期限数
    fileprivate let millionLabel = UILabel() //万元
    fileprivate let monthLabel = UILabel() //个月
    
    fileprivate let supplyLabel = UILabel() //月供
    fileprivate let supplyAmountLabel = UILabel() //月供金额
    fileprivate let supplyYuanLabel = UILabel() //元
    fileprivate let loanLineView = UIView()
    
    fileprivate let loanFreeLabel = UILabel() //贷款费用
    fileprivate let loanAmountLabel = UILabel() //贷款费用数
    fileprivate let loanYuanLabel = UILabel() //元
    
    fileprivate let interestLabel = UILabel() //利率
    fileprivate let ratesLabel = UILabel() //利率数
    
    fileprivate let managementLineView = UIView()
    
    fileprivate let managementLabel = UILabel() //管理费
    fileprivate let managementFreeLabel = UILabel() //管理费数量
    fileprivate let managementYuanLabel = UILabel() //元
    
    fileprivate let onceLabel = UILabel() //一次性费用
    fileprivate let onceFreeLabel = UILabel() //一次性费用数
    fileprivate let onceYuanLabel = UILabel() //元
    
    
    init(model:GenericLoansModel) {
        super.init(frame: .zero)
        
        self.model = model
        
        amountTextView.delegate = self
        amountTextView.keyboardType = .numberPad
        amountTextView.text = "1"
        amountTextView.placeholder = "0"
        amountTextView.font = fontSize
        amountTextView.layer.borderColor = UIColor.lineColor().cgColor
        amountTextView.layer.borderWidth = 1
        self.addSubview(amountTextView)
        
        amountLabel.text = loanAmount + ":"
        amountLabel.font = fontSize
        self.addSubview(amountLabel)
        
        millionLabel.text = millionYuan
        millionLabel.textColor = UIColor.gray
        millionLabel.font = fontSize
        amountTextView.addSubview(millionLabel)
        
        timeLabel.text = durationLoan + ":"
        timeLabel.font = fontSize
        self.addSubview(timeLabel)
        
        timeTextView.delegate = self
        timeTextView.keyboardType = .numberPad
        timeTextView.text = "12"
        timeTextView.placeholder = "0"
        timeTextView.font = fontSize
        timeTextView.layer.borderColor = UIColor.lineColor().cgColor
        timeTextView.layer.borderWidth = 1
        self.addSubview(timeTextView)
        
        monthLabel.text = months
        monthLabel.textColor = UIColor.gray
        monthLabel.font = fontSize
        timeTextView.addSubview(monthLabel)
        
        loanLineView.backgroundColor = UIColor.black
        self.addSubview(loanLineView)
        
        supplyYuanLabel.text = yuan
        supplyYuanLabel.font = fontSize
        self.addSubview(supplyYuanLabel)
        
        supplyAmountLabel.text = "0.00"
        supplyAmountLabel.textColor = UIColor.orange
        supplyAmountLabel.font = UIFont.systemFont(ofSize: RATIO_H(maxNum: 20))
        self.addSubview(supplyAmountLabel)
        
        supplyLabel.text = monthlySupply + ":"
        supplyLabel.font = fontSize
        self.addSubview(supplyLabel)
        
        loanFreeLabel.text = loanfee + ":"
        loanFreeLabel.font = fontSize
        self.addSubview(loanFreeLabel)
        
        loanAmountLabel.text = "0.00"
        loanAmountLabel.textColor = UIColor.orange
        loanAmountLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(loanAmountLabel)
        
        loanYuanLabel.text = yuan
        loanYuanLabel.font = fontSize
        self.addSubview(loanYuanLabel)
        
        interestLabel.text = model.rate_unit! + interestRates + ":"
        interestLabel.font = fontSize
        self.addSubview(interestLabel)
        
        ratesLabel.text = model.interest_rate! + "%"
        ratesLabel.textColor = UIColor.darkGray
        ratesLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(ratesLabel)
        
        managementLineView.backgroundColor = UIColor.black
        self.addSubview(managementLineView)
        
        managementYuanLabel.text = yuan
        managementYuanLabel.font = fontSize
        self.addSubview(managementYuanLabel)
        
        managementFreeLabel.text = "0"
        managementFreeLabel.textColor = UIColor.darkGray
        managementFreeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(managementFreeLabel)
        
        managementLabel.text = managementFee + ":"
        managementLabel.font = fontSize
        self.addSubview(managementLabel)
        
        onceLabel.text = onceFee + ":"
        onceLabel.font = fontSize
        self.addSubview(onceLabel)
        
        onceFreeLabel.text = "0"
        onceFreeLabel.textColor = UIColor.darkGray
        onceFreeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 20))
        self.addSubview(onceFreeLabel)
        
        onceYuanLabel.text = yuan
        onceYuanLabel.font = fontSize
        self.addSubview(onceYuanLabel)
        
        
        amountTextView.snp.makeConstraints { (make) in
            make.right.equalTo(self.snp.centerX).offset(RATIO(maxNum: -5))
            make.height.equalTo(30)
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.width.equalTo(RATIO(maxNum: 100))
        }
        
        millionLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        amountLabel.snp.makeConstraints { (make) in
            make.right.equalTo(amountTextView.snp.left)
            make.centerY.equalTo(amountTextView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.centerX).offset(RATIO(maxNum: 5))
            make.centerY.equalTo(amountTextView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        timeTextView.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right)
            make.width.height.centerY.equalTo(amountTextView)
        }
        
        monthLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        loanLineView.snp.makeConstraints { (make) in
            make.top.equalTo(amountTextView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.centerX.equalToSuperview()
            make.width.equalTo(1)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        supplyYuanLabel.snp.makeConstraints { (make) in
            make.right.equalTo(loanLineView.snp.centerX).offset(RATIO(maxNum: -5))
            make.bottom.equalTo(loanLineView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        supplyAmountLabel.snp.makeConstraints { (make) in
            make.right.equalTo(supplyYuanLabel.snp.left)
            make.bottom.equalTo(supplyYuanLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        supplyLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(supplyAmountLabel)
            make.right.equalTo(supplyAmountLabel.snp.left)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        loanFreeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(loanLineView.snp.right).offset(RATIO(maxNum: 5))
            make.bottom.equalTo(loanLineView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        loanAmountLabel.snp.makeConstraints { (make) in
            make.left.equalTo(loanFreeLabel.snp.right)
            make.bottom.equalTo(loanFreeLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        loanYuanLabel.snp.makeConstraints { (make) in
            make.left.equalTo(loanAmountLabel.snp.right)
            make.bottom.equalTo(loanAmountLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        interestLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self.loanLineView.snp.centerX)
            make.top.equalTo(loanLineView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        ratesLabel.snp.makeConstraints { (make) in
            make.left.equalTo(interestLabel.snp.right)
            make.bottom.equalTo(interestLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        managementLineView.snp.makeConstraints { (make) in
            make.centerX.height.width.equalTo(loanLineView)
            make.top.equalTo(ratesLabel.snp.bottom).offset(RATIO_H(maxNum: 20))
        }
        
        managementYuanLabel.snp.makeConstraints { (make) in
            make.right.equalTo(managementLineView.snp.centerX).offset(RATIO(maxNum: -5))
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
            make.bottom.equalTo(managementLineView)
        }
        
        managementFreeLabel.snp.makeConstraints { (make) in
            make.right.equalTo(managementYuanLabel.snp.left)
            make.bottom.equalTo(managementYuanLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        managementLabel.snp.makeConstraints { (make) in
            make.right.equalTo(managementFreeLabel.snp.left)
            make.bottom.equalTo(managementFreeLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        onceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(managementLineView.snp.right).offset(RATIO(maxNum: 5))
            make.bottom.equalTo(managementLineView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        onceFreeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(onceLabel)
            make.left.equalTo(onceLabel.snp.right)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        onceYuanLabel.snp.makeConstraints { (make) in
            make.left.equalTo(onceFreeLabel.snp.right)
            make.bottom.equalTo(onceFreeLabel)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        //注册输入框监听通知
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextChange), name: .UITextFieldTextDidChange, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(textFieldTextChange), name: .UITextFieldTextDidChange, object: nil)
        
        textFieldTextChange()
        
    }
    
    //MARK: 监听文本，计算贷款利息
    @objc fileprivate func textFieldTextChange() {
        
        if (Double(amountTextView.text!) ?? 0) > (Double(model.money ?? "") ?? 0) {
            amountTextView.text = model.money
            KMTool.showText(_text: "贷款金额不能超过\(model.money ?? "")万元")
        }
        
        if amountTextView.text == "" || timeTextView.text == "" || amountTextView.text == "0" || timeTextView.text == "0" {
            
            supplyAmountLabel.text = "0.00"
            loanAmountLabel.text = "0.00"
            
            return
        }
        
        
        
        switch model.rate_unit {
        case "年":
            //贷款金额
            let loanAmount = (Double(amountTextView.text!+"0000")! * Double((ratesLabel.text?.replacingOccurrences(of: "%", with: ""))!)! * Double(timeTextView.text!)! * 0.01)/12
            //月供
            let month = (Double(amountTextView.text!+"0000")! + loanAmount)/Double(timeTextView.text!)!
            loanAmountLabel.text = "\(String(format: "%.2f", loanAmount))"
            supplyAmountLabel.text = "\(String(format: "%.2f", month))"
        case "月":
            //贷款金额
            let loanAmount = Double(amountTextView.text!+"0000")! * Double((ratesLabel.text?.replacingOccurrences(of: "%", with: ""))!)! * Double(timeTextView.text!)! * 0.01
            //月供
            let month = (Double(amountTextView.text!+"0000")! + loanAmount)/Double(timeTextView.text!)!
            loanAmountLabel.text = "\(String(format: "%.2f", loanAmount))"
            supplyAmountLabel.text = "\(String(format: "%.2f", month))"
        case "日":
            //一天利息
            let daily = Double(amountTextView.text!+"0000")! *  Double((ratesLabel.text?.replacingOccurrences(of: "%", with: ""))!)! * 0.01
            //贷款金额
            let loanAmount = Double(timeTextView.text!)! * daily * 30
            //月供
            let month = (Double(amountTextView.text!+"0000")! + loanAmount)/Double(timeTextView.text!)!
            loanAmountLabel.text = "\(String(format: "%.2f", loanAmount))"
            supplyAmountLabel.text = "\(String(format: "%.2f", month))"
        default:
            break
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoanDetailsTextView : UITextFieldDelegate {
    
    //MARK: 输入款限制
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if textField.text == "0" {
            if range.length == 1 && string.count == 0 {
                return true
            }else{
                return false
            }
        }
        
        if textField == self.amountTextView {
            if (textField.text?.count)! >= 5 {
                
                if range.length == 1 && string.count == 0 {
                    return true
                }else{
                   return false
                }
            }
        }
        
        if textField == self.timeTextView {
            if ((textField.text as NSString?)?.length)! >= 3 {
                
                if range.length == 1 && string.count == 0 {
                    return true
                }else{
                    return false
                }
            }
        }
        
        
        
        return true
    }
}
